﻿Imports System.Web.UI
Imports SISSAT.WebDevelopment.Views
Imports SISSAT.Administration.Services
Imports SISSAT.Administration.DomainModels

Public Class Index
    Inherits Page

    Property Service As UserService
    Public Property ErrorMessage As String

    Property Username As String
        Get
            Return usernameTextBox.Text
        End Get
        Set(value As String)
            usernameTextBox.Text = value
        End Set
    End Property

    Property Password As String
        Get
            Return passwordTextBox.Text
        End Get
        Set(value As String)
            passwordTextBox.Text = value
        End Set
    End Property

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Service = New UserService
        usernameTextBox.Focus()
    End Sub

    Protected Sub SubmitButtonClick() Handles submitButton.Click
        If String.IsNullOrEmpty(Username) AndAlso String.IsNullOrEmpty(Password) Then
            ErrorMessage = "Username and Password is required."
            Return
        ElseIf String.IsNullOrEmpty(Username) Then
            ErrorMessage = "Username is required."
            Return
        ElseIf String.IsNullOrEmpty(Password) Then
            ErrorMessage = "Password is required."
            Return
        End If

        Dim currentUser As SouthdaleUser = Service.AuthenticateUser(Username, Password)

        If currentUser Is Nothing Then
            ClearButtonClick()
            ErrorMessage = "Username and Password doesn't match."
            Return
        End If

        SISSATPageBase.User = currentUser
        HttpContext.Current.Response.Redirect("/Pages/Home/Index.aspx")
    End Sub

    Protected Sub ClearButtonClick() Handles clearButton.Click
        Username = String.Empty
        Password = String.Empty
        ErrorMessage = String.Empty
    End Sub

End Class